﻿// ===================================
// Author : Micro.SanVey
// Eamil  : sanvey.china@gmail.com
// ===================================

namespace USpell.API
{
    #region 引用
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using USpell.Models;
    using U.Help.Local;
    using WeiboSdk.PageViews;
    using WeiboSdk;
    using U.Help.Common;
    #endregion

    /// <summary>
    /// USpell 核心功能API
    /// </summary>
    public static class Core
    {

        public static bool BindingSinaFriend()
        {
            SdkData.AppKey = StaticSource.SinaKey;
            SdkData.AppSecret = StaticSource.SinaSecret;
            SdkData.RedirectUri = StaticSource.SinaRedirectUri;

            string mySinaToken = Isolated.GetSetting(StaticSource.SinaToken) as string;
            string mySinaExpries = Isolated.GetSetting(StaticSource.SinaExpries) as string;
            if (!string.IsNullOrEmpty(mySinaExpries) && !string.IsNullOrEmpty(mySinaToken))
            {
                return true;
            }

            AuthenticationView.OAuth2VerifyCompleted = (e1, e2, e3) => {
                if (e2.errCode == SdkErrCode.SUCCESS)
                {
                    if (null != e3)
                    {
                        Isolated.SaveSetting(StaticSource.SinaToken, e3.accesssToken);
                        Isolated.SaveSetting(StaticSource.SinaExpries, e3.expriesIn);

 
                    }
                }
            };
            AuthenticationView.OBrowserCancelled = (e1, e2) => { };
            return false;
        }
       


        /// <summary>
        /// 创建一定数量的生词
        /// </summary>
        /// <param name="num">创建数量</param>
        /// <returns></returns>
        public static List<string> CreateNewWords(int num)
        {
            return new List<string> { "枕头", "台灯", "超级本" };
            //todo
        }

        /// <summary>
        /// 给指定好友发送猜词消息
        /// </summary>
        /// <param name="guess">消息</param>
        /// <param name="friend">好友</param>
        public static void SendSpellToFriend(Guess guess, Friend friend)
        {
            //todo
        }

        /// <summary>
        /// 获取所有好友给我的猜题
        /// </summary>
        /// <returns></returns>
        public static List<Guess> GetAllGuessFromFriend()
        {
            try
            {
                SQLiteHelper db = new SQLiteHelper(StaticSource.DB_NAME);
                string sql = "select * from Guess where Type=" + StaticSource.GuessType.form.GetHashCode();
                var guesses = db.SelectList<Guess>(sql);
                return guesses;
            }
            catch (Exception ex)
            {
                //todo log
                var msg = ex.Message;
                return null;
            }
        }

        public static List<Guess> GetAllGuessToFriend()
        {
            try
            {
                SQLiteHelper db = new SQLiteHelper(StaticSource.DB_NAME);
                string sql = "select * from Guess where Type=" + StaticSource.GuessType.to.GetHashCode();
                var guesses = db.SelectList<Guess>(sql);
                return guesses;
            }
            catch (Exception ex)
            {
                //todo log
                var msg = ex.Message;
                return null;
            }
        }

    }
}
